function recoverTree(root: TreeNode | null): void {
  let t1: TreeNode | null = null,
    t2: TreeNode | null = null,
    prev: TreeNode | null = null;
  const inorder = (node: TreeNode | null) => {
    if (node === null) return;
    inorder(node.left);

    if (prev !== null && prev.val > node.val) {
      if (t1 === null) t1 = prev;
      t2 = node;
    }

    prev = node;
    inorder(node.right);
  };

  inorder(root);
  let temp = t1!.val;
  t1!.val = t2!.val;
  t2!.val = temp;
}

